package com.dongdong.realm;

import com.dongdong.model.TbUser;
import com.dongdong.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class UserLoginRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;

        TbUser tbUser = userService.getUserByName(token.getUsername());

        if (tbUser==null){
            return null;
        }
        SimpleAuthenticationInfo sim = new SimpleAuthenticationInfo(tbUser,tbUser.getPassword(), ByteSource.Util.bytes(tbUser.getSalt()),"userRole");
        return sim;
    }
}
